const config = require('./config/config')
const Koa = require('koa')
const path = require('path')
const koaStatic = require('koa-static')
// const ejs = require('ejs');
const cors = require('koa2-cors');        // 跨域
const views = require('koa-views');
const router = require('./app/router.js')
const app = new Koa()

// 配置静态资源加载中间件
app.use(koaStatic(
    path.join(__dirname , './static')
))

// 配置模板文件目录和后缀名
app.use(views(__dirname + '/app/pages', {
  extension: 'ejs'
}))
// app.use( async ( ctx ) => {
//     // let title = 'hello koa2'
//     await ctx.render('about')
// })

// 具体参数我们在后面进行解释
app.use(cors({
    origin: function (ctx) {
        if (ctx.url === '/test') {
            return "*"; // 允许来自所有域名请求
        }
        return 'http://localhost:8080'; // 这样就能只允许 http://localhost:8080 这个域名的请求了
    },
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
    maxAge: 5,
    credentials: true,
    allowMethods: ['GET', 'POST', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))

app.use(router());

app.listen(config.port, ()=>{
    console.log('service is 3000')
})